import 'package:dio_log/dio_log.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:flutter_ncsjqz_getx/widget/ink_widget.dart';
import 'package:flutter_ncsjqz_getx/widget/input_widget.dart';
import 'package:get/get.dart';

import 'farm_search_logic.dart';

/// <summary>
/// todo: 农场搜索
/// author：zwb
/// dateTime： 2021/11/26 16:35
/// filePath： lib/page/farm/search/farm_search_view.dart
/// desc:
/// <summary>
///
class FarmSearchPage extends StatefulWidget {
  @override
  _FarmSearchPageState createState() => _FarmSearchPageState();
}

class _FarmSearchPageState extends State<FarmSearchPage> {
  final logic = Get.put(FarmSearchLogic());
  String search = "";
  List<String> listData = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getData();
  }
  getData() async{
    search = (SpUtil.getString("search",))!;
    if(search != ""){
      search.contains(",") ? listData = search.split(",") : listData.add(search);
      listData.reversed;
    }
    print("搜索词缓存：${listData}");
    logic.update();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body:GetBuilder<FarmSearchLogic>(
        builder: (controller) {
          return SafeArea(
              child: Padding(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        InkWell(
                          child: Icon(Icons.arrow_back_ios,size: 25,),
                          onTap: (){
                            Get.back(result: logic.textEditingController.value.text,);
                          },
                        ),
                        SizedBox(width: 5,),
                        Expanded(
                          child: textFieldWidget(
                            controller: logic.textEditingController,
                            circular: 5,
                            hintText: "搜索关键词",
                            hintStyle: TextStyle(color: Colors.grey),
                            onChanged: (v)=> logic.update(),
                            onSubmitted: (v){
                              if(v=="testDio") return Navigator.of(context).push(MaterialPageRoute(builder: (context) => HttpLogListWidget(),),);
                              if(v!="" && v!="null" && v!="Null" && v!="NULL"){
                                search != ""  ? search += "," : search = v;
                                SpUtil.putString("search", search)!.then((value) => print("存储->$value"));
                              }
                              Get.back(result: logic.textEditingController.value.text);
                            },
                            prefixIcon: Padding(
                              padding: EdgeInsets.only(left: 8,),
                              child: Icon(Icons.search,color: Colors.grey,),
                            ),
                            suffixIcon:logic.textEditingController.value.text != "" ?InkWell(
                              child: Column(
                                mainAxisAlignment: MainAxisAlignment.center,
                                children: [
                                  Container(
                                    width: 25,
                                    height: 25,
                                    decoration: BoxDecoration(
                                      borderRadius: BorderRadius.circular(30),
                                      border: Border.all(color: Colors.grey,),
                                    ),
                                    child: Icon(Icons.close,color: Colors.grey,),
                                  ),
                                ],
                              ),
                              onTap: (){
                                logic.textEditingController.clear();
                                logic.update();
                              },
                            ):SizedBox(),
                          ),
                        ),
                      ],
                    ),

                    Container(
                      margin: EdgeInsets.symmetric(vertical: 10),
                      decoration: BoxDecoration(
                        border: Border(left: BorderSide(color: Colors.green,width: 2),),
                      ),
                      child: InkWidget.text(" 搜索历史",color: Colors.green),
                    ),

                    Expanded(
                      child: ListView(
                        shrinkWrap: true,
                        children: [
                          Wrap(
                            spacing: 10.0,
                            runSpacing: 0.0,
                            children: <Widget>[
                              ...List.generate(listData.length, (index){
                                return OutlineButton(
                                  onPressed: () {  },
                                  child: InkWidget.text("${listData[index]}",color: Colors.green.shade500),
                                  borderSide: BorderSide(color: Colors.green),
                                );
                              })
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
                padding: EdgeInsets.all(15),
              )
          );
        },
      ),
    );
  }

  @override
  void dispose() {
    Get.delete<FarmSearchLogic>();
    super.dispose();
  }
}